package edu.guet.server.controller;

import edu.guet.server.model.dto.HomeworkDTO;
import edu.guet.server.model.vo.HomeworkDetailsVO;
import edu.guet.server.model.vo.HomeworkListVO;
import edu.guet.server.model.vo.SemHmkTestVO;
import edu.guet.server.model.vo.StuTestListVO;
import edu.guet.server.service.StudentHomeworkService;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class StudentHomeworkController {

    @Autowired
    private StudentHomeworkService homeworkService;

    /**
     * 获取作业列表
     * @return
     */
//    @GetMapping("/assignments")
//    public ResultAPI getHomeworkList(@RequestParam int user_id){
//        List<HomeworkListVO> homeworkVO = homeworkService.getHomeworkList(user_id);
//        return ResultAPI.success(homeworkVO);
//    }
    @GetMapping("/assignments")
    public ResultAPI getHomeworkList(@RequestParam int semester_id,@RequestParam int user_id){
        List<SemHmkTestVO> semHmkTestVOS = homeworkService.getHomeworkList(semester_id,user_id);
        log.info("semHmkTestVOS:{}", semHmkTestVOS);
        return ResultAPI.success(semHmkTestVOS);
    }

    /**
     * 获得单个作业阶段
     * @param user_id
     * @param hmk_id
     * @return
     */
    @GetMapping("/assignments/hmkstage")
    public ResultAPI gethmkStage(@RequestParam int user_id, @RequestParam int hmk_id){
        HomeworkListVO homeworkListVO=homeworkService.getHmkStage(user_id,hmk_id);
        log.info("homeworkListVO:{}", homeworkListVO);
        return ResultAPI.success(homeworkListVO);
    }

    /**
     * 获得单个测试阶段
     * @param user_id
     * @param test_id
     * @return
     */
    @GetMapping("/assignments/teststage")
    public ResultAPI gettestStage(@RequestParam int user_id,@RequestParam int test_id){
        StuTestListVO stuTestListVO =homeworkService.getTestStage(user_id,test_id);
        log.info("stuTestListVO:{}", stuTestListVO);
        return ResultAPI.success(stuTestListVO);
    }
    /**
     * 通过id获取作业详情
     * @param id
     * @return
     */
    @GetMapping("/assignments/{id}/{user_id}")
    public ResultAPI getHomeworkDetails(@PathVariable int id, @PathVariable int user_id){
        HomeworkDetailsVO detailsVO = homeworkService.getHomeworkDetails(id, user_id);
        return ResultAPI.success(detailsVO);
    }

    /**
     * 提交作业
     * @param assignmentId
     * @param content
     * @param file
     * @return
     */
    @PostMapping("/assignments/submit")
    public ResultAPI submitHomework(@RequestParam(value = "file", required = false) MultipartFile file,
                                    @RequestParam("assignmentId") int assignmentId,
                                    @RequestParam("content") String content
                                    ){
        HomeworkDTO homeworkDTO = new HomeworkDTO();
        homeworkDTO.setAssignmentId(assignmentId);
        homeworkDTO.setContent(content);
        homeworkDTO.setFile(file);
        homeworkDTO.setUserId(1);
        //附件也可以不交，所以不用判断
//        if(file != null && !file.isEmpty()){
            homeworkService.submitHomework(homeworkDTO, file);
            return ResultAPI.success(null, "提交成功");
////        }
//        return ResultAPI.error("提交失败！");
    }
}
